<template>
	<view class="wrap">
		<u-gap height="20" bg-color="#f5f5f5"></u-gap>
		<u-cell-group :border="false">
			<u-cell-item title="通用" @click="navTo('/pages/sys/user/currency')" :arrow="true"></u-cell-item>
			<u-gap height="20" bg-color="#f5f5f5"></u-gap>
			<!-- <u-cell-item title="版本更新"  @click="navTo('')" :arrow="true"></u-cell-item> -->
			<u-cell-item title="版本更新" @click="updateApp" :arrow="true"></u-cell-item>
			<u-cell-item title="隐私政策" @click="navTo('')" :arrow="true"></u-cell-item>
			<u-cell-item title="用户协议" @click="navTo('')" :arrow="true"></u-cell-item>
		</u-cell-group>
		<u-gap height="20" bg-color="#f5f5f5"></u-gap>
		<view>
			<u-button class="sign-out" @click="logout" :hair-line="false">退出登录</u-button>
		</view>
		<u-modal v-model="isAppModule.show" :content="isAppModule.content" :show-cancel-button="true"
			@confirm="isAppModuleConfirm"></u-modal>
		<u-modal v-model="downloadModel.show" :title-style="{color: 'red'}" :show-confirm-button="false"
			:show-cancel-button="true" @cancel="downloadModelCancel">
			<view class="slot-content">
				<view>{{downloadModel.content}}</view>
				<!-- <rich-text :nodes="downloadModel.content"></rich-text> -->
			</view>
		</u-modal>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	/**
	 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
	 */
	export default {
		data() {
			return {
				_prg: 0,
				_showLoading: null,
				_dtask: null,
				downloadModel: {
					show: false,
					content: ''
				},
				isAppModule: {
					show: false,
					content: '检测到应用可能不是最新版本，您是否更新？',
					downloadUrl: ''
				},
			};
		},
		methods: {
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			openSettings() {
				// #ifdef APP-PLUS
				uni.getSystemInfo({
					success(res) {
						if (res.platform == 'ios') {
							plus.runtime.openURL("app-settings://");
						} else if (res.platform == 'android') {
							var main = plus.android.runtimeMainActivity();
							var Intent = plus.android.importClass("android.content.Intent");
							var mIntent = new Intent('android.settings.SOUND_SETTINGS');
							main.startActivity(mIntent);
						}
					}
				});
				// #endif
				// #ifndef APP-PLUS
				this.$u.toast('小程序端或H5端已是最新版，无需检查更新！');
				// #endif
			},
			logout() {
				this.$u.api.logout().then(res => {
					this.$u.toast(res.msg);
					if (res.code == '200') {
						let self = this;
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/sys/login/index'
							});
						}, 500);
					}
				});
			},
			getVersionList(params) {
				
				
				return new Promise((resolve, reject) => {
					this.$u.api.verison.list({
						pageSize: 1,
						pageNum: 1
					}).then(res => {
						resolve(res)
					})
				})
			},
			async updateApp() {
				let that = this
				plus.runtime.getProperty(plus.runtime.appid, async (wgtinfo) => {
					// //appid属性
					// var wgtStr = "appid:"+wgtinfo.appid;
					// //version属性
					// wgtStr += "<br/>version:"+wgtinfo.version;
					// //name属性
					// wgtStr += "<br/>name:"+wgtinfo.name;
					// //description属性
					// wgtStr += "<br/>description:"+wgtinfo.description;
					// //author属性
					// wgtStr += "<br/>author:"+wgtinfo.author;
					// //email属性
					// wgtStr += "<br/>email:"+wgtinfo.email;
					// //features 属性
					// wgtStr += "<br/>features:"+wgtinfo.features;
					console.log('version:', wgtinfo.version);

					let data = (await this.getVersionList()).rows[0]
					console.log(data);
					if (data.code == wgtinfo.version) {
						this.$refs.uToast.show({
							title: '您已经是最新版本',
							type: 'success'
						})
						return
					}else{
						let downloadApkUrl = ''
						if (data.appUrl.indexOf('121.196.62.34:8065') != -1) {
							downloadApkUrl = data.appUrl
						} else {
							downloadApkUrl = this.vuex_config.baseUrl + data.appUrl.substring(data.appUrl
								.indexOf('/profile'))
						}
						this.isAppModule.show = true
						this.isAppModule.downloadUrl = downloadApkUrl
					}
				});
			},
			isAppModuleConfirm(){
				const downloadApkUrl = this.isAppModule.downloadUrl
				this._dtask = plus.downloader.createDownload(downloadApkUrl, {}, function(d, status) {
					// 下载完成  
					if (status == 200) {
						console.log(plus.io.convertLocalFileSystemURL(d.filename));
						plus.runtime.install(plus.io.convertLocalFileSystemURL(d
							.filename), {}, {}, function(
							error) {
							uni.showToast({
								title: '安装失败',
								duration: 1500
							});
						})
					} else {
						uni.showToast({
							title: '更新失败',
							duration: 1500
						});
					}
				});
				try {
					this._dtask.start(); // 开启下载的任务
					this._prg = 0;
					// this._showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象 
					this.downloadModel.show = true
					this.downloadModel.content = '正在下载'
					this._dtask.addEventListener('statechanged', this.stateChanged, false);
				} catch (err) {
					console.log('错误信息', err)
					plus.nativeUI.closeWaiting();
				}
			},
			stateChanged(task, status) {
				// console.log(task.state);
				// console.log(this.prg);.
				// 给下载任务设置一个监听 并根据状态  做操作
				// console.log(this.downloadModel.content);
				switch (task.state) {
					case 1:
						// this._showLoading.setTitle("正在下载");
						this.downloadModel.content = '正在下载'
						break;
					case 2:
						// this._showLoading.setTitle("已连接到服务器");
						this.downloadModel.content = '已连接到服务器'
						break;
					case 3:
						this._prg = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) *
							100);
						// this._showLoading.setTitle("  正在下载" + this._prg + "%  ");
						this.downloadModel.content = "正在下载" + this._prg + "% "
						break;
					case 4:
						plus.nativeUI.closeWaiting();
						this.downloadModel.show = false
						//下载完成
						break;
				}
			},
			downloadModelCancel() {
				plus.downloader.clear();
			},
		}
	};
</script>
<style lang="scss">
	page {
		background-color: #f5f5f5;
	}

	/deep/ .u-cell-title {
		padding: 25rpx 30rpx;
		font-size: 30rpx;
	}

	.slot-content {
		text-align: center;

		view {
			padding: 40upx 0;
		}
	}
</style>
